package com.hxd.mall.module.usercenter.fragment

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectFragment
import com.hxd.mall.module.usercenter.home.OrderListBean
import com.hxd.mall.module.usercenter.order.IOrderQueryPresenter
import com.hxd.mall.module.usercenter.order.IOrderQueryView
import com.hxd.mall.module.usercenter.order.OrderItemAdapter
import com.hxd.mall.utils.open.initRecyclerView
import kotlinx.android.synthetic.main.view_pager_order_layout.*

/**
 * Create by yinzhengwei on 2019-11-05
 * @Function
 */
class PageFragment(var status: String, var index: Int) : BaseInjectFragment<IOrderQueryPresenter>(), IOrderQueryView {

    private var pageNum = 1
    private var pageSize = 20
    private val adapter = OrderItemAdapter()
    private var moreView: View? = null

    override fun attachLayoutRes() = R.layout.view_pager_order_layout

    override fun initViews() {
        orderList.initRecyclerView(adapter, padding = 13)

        //构造无更多数据的布局
        moreView = LayoutInflater.from(activity!!).inflate(R.layout.no_date_layout, null)

        //构造空数据的view
        val view = LayoutInflater.from(context).inflate(R.layout.empty_order_layout, null)
        adapter.emptyView = view

        view.findViewById<TextView>(R.id.tv_default).text = when (index) {
            0 -> resources.getString(R.string.order_empty)
            1 -> resources.getString(R.string.order_empty_start)
            2 -> resources.getString(R.string.order_empty_ing)
            3 -> resources.getString(R.string.order_empty_end)
            else -> ""
        }

        //在内容不满一页的时候，是否可以上拉加载更多
//        smart_refresh_layout.setEnableLoadMoreWhenContentNotFull(false)

        smart_refresh_layout.setOnRefreshListener {
            pageNum = 1
            initData()
        }
        smart_refresh_layout.setOnLoadMoreListener {
            pageNum++
            initData()
        }
    }

    override fun initData() {
        mPresenter?.getData(pageNum, pageSize, status)
    }

    override fun getListInfo(orderListBean: OrderListBean) {
        orderListBean?.data?.list?.run {
            if (this.isNotEmpty()) {
                if (pageNum == 1) {
                    adapter.setNewData(this)
                } else {
                    adapter.addData(this)
                }
            }
            adapter.removeAllFooterView()
            //结束刷新
            if (this.isEmpty() || this.size < pageSize) {
                if (pageNum != 1) adapter.addFooterView(moreView)
                smart_refresh_layout?.setEnableLoadMore(false)
            } else {
                smart_refresh_layout?.setEnableLoadMore(true)
            }
        }
    }

    override fun finishRefresh() {
        smart_refresh_layout.finishRefresh()
        smart_refresh_layout.finishLoadMore()
    }

    override fun finishFailRefresh() {
        smart_refresh_layout.finishRefresh()
        adapter.loadMoreFail()
    }
}